package com.zhoux.api.io.raf;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.io.UnsupportedEncodingException;

/**
 * @author taoyi
 * @version 1.0
 * @date 2021/3/25 10:30
 *
 *  本类用于测试向文件中写入文本数据
 *
 */
public class WriteString_Test {
    public static void main(String[] args) {
        RandomAccessFile raf = null;
        String line = "愿你迷路到我身旁！";
        try {
            raf = new RandomAccessFile(".\\src\\main\\java\\com\\zhoux\\file\\test.txt","rw");
            byte[] data = line.getBytes("utf-8");
            raf.write(data);
            System.out.println("写入成功!");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                raf.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

